package com.example.testall;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.io.*;




import java.io.File;


/**
 * @author xxg
 */
@Slf4j
public class FileCodeTest {

    //58556  e4bc
    //58757    0xefbb     efbb
    //
    @Test
    public  void t01(){
        final String s = Integer.toHexString(58757);
        System.out.println(s);
        final String s2 = Integer.toHexString(0xefbb);
        System.out.println(s2);

        int valueTen2 = Integer.parseInt("e585",16);
        System.out.println(valueTen2);
    }

    /**
     * 获得文件编码
     * @param fileName
     * @return
     * @throws Exception
     */
    public static String codeString(String fileName) throws Exception {
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
        int p = (bin.read() << 8) + bin.read();
        bin.close();
        String code = null;
        switch (p) {
            case 0xefbb:
                code = "UTF-8-BOM";
                break;
            case 0xfffe:
                code = "Unicode";
                break;
            case 0xfeff:
                code = "UTF-16BE";
                break;
            case   58757:
                code="UTF-8";
                break;
            case 47531:
                code="ANSI";
                break;
            default:
                code = "GBK";
        }
        return code;
    }

    public static void main(String[] args) throws Exception {
        String path="C:\\Users\\Administrator.DESKTOP-GTF5LQT\\Desktop\\临时文件\\sftp企业列表-整型.txt";
        final File file = new File(path);
        System.out.println(file.getPath());
        System.out.println(file.getAbsolutePath());
        final String s = codeString(file.getPath());
        System.out.println(s);
    }

}
